package com.aidansmeaton.weightmanager;

import android.app.TabActivity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

/** The first activity in the app. */
public class InspirationActivity extends TabActivity {
		
	private TabHost tabHost;
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.inspiration);
        
        // Set up tabs
	    setupTabHost();
	    //startingTab = 0;
	            
        // Show tabs and date
        updateDisplay();

        // Stop soft keyboard popping up automatically
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
	    
	}
    
    public void resume(){
    	onResume();
    }
    
    @Override
    protected void onResume() {
    	super.onResume();
    }

    @Override
    protected void onStop() {
    	super.onStop();
    	saveDefaultTab();
    }
        
    private int defaultTab(){
    	return getSharedPreferences("Inspiration", 0).getInt("Tab", 0);
    }
    
    private void saveDefaultTab(){
    	getSharedPreferences("Inspiration", 0).edit().putInt("Tab", tabHost.getCurrentTab()).commit();
    }     
            
    // updates the date we display in the TextView
    private void updateDisplay() {     
    	setupTabHost();
    	updateTabContents();
    }
    
    private void updateTabContents(){
    	
    	tabHost.setCurrentTab(0);
    	tabHost.clearAllTabs();
    	
    	Intent intent = new Intent().setClass(this, RedditActivity.class);
	    intent.putExtra("/r", "loseit");
	    intent.putExtra("Message", getString(R.string.inspiration_loseit));
	    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
	    setupTab(getResources().getDrawable(R.drawable.ic_weight), "LoseIt", intent);
	    
	    intent = new Intent().setClass(this, RedditActivity.class);
	    intent.putExtra("/r", "fitmeals");
	    intent.putExtra("Message", getString(R.string.inspiration_fitmeals));
	    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
	    setupTab(getResources().getDrawable(R.drawable.ic_food), "FitMeals", intent);
	    
	    intent = new Intent().setClass(this, RedditActivity.class);
	    intent.putExtra("/r", "fitness");
	    intent.putExtra("Message", getString(R.string.inspiration_fitness));
	    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
	    setupTab(getResources().getDrawable(R.drawable.ic_exercise), "Fitness", intent);
	    
	    intent = new Intent().setClass(this, RedditActivity.class);
	    intent.putExtra("/r", "motivation");
	    intent.putExtra("Message", getString(R.string.inspiration_motivation));
	    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
	    setupTab(getResources().getDrawable(R.drawable.ic_mood), "Motivation", intent);
	    
	    intent = new Intent().setClass(this, RedditActivity.class);
	    intent.putExtra("/r", "progresspics");
	    intent.putExtra("Message", getString(R.string.inspiration_progresspics));
	    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
	    setupTab(getResources().getDrawable(R.drawable.ic_mirror), "ProgressPics", intent);
	    
	    tabHost.setCurrentTab(defaultTab());
	    
    }
    
    /**
	 * @author Josh Clemm
	 * @see http://code.google.com/p/android-custom-tabs/
	 * @see http://joshclemm.com/blog/?p=136
	 */
	private void setupTab(final Drawable icon, String tag, Intent intent) {
		View tabview = createTabView(tabHost.getContext(), icon);
		TabSpec setContent = tabHost.newTabSpec(tag).setIndicator(tabview).setContent(intent);
		tabHost.addTab(setContent);
	}

	/**
	 * @author Josh Clemm
	 * @see http://code.google.com/p/android-custom-tabs/
	 * @see http://joshclemm.com/blog/?p=136
	 */
	private void setupTabHost() {
		tabHost = (TabHost) findViewById(android.R.id.tabhost);
		tabHost.setup();
	}
	
	/**
	 * @author Josh Clemm
	 * @see http://code.google.com/p/android-custom-tabs/
	 * @see http://joshclemm.com/blog/?p=136
	 */
	private static View createTabView(final Context context, final Drawable icon) {
		View view = LayoutInflater.from(context).inflate(R.layout.tabs_bg, null);
		
		ImageView iv = (ImageView) view.findViewById(R.id.tabIcon);
		iv.setImageDrawable(icon);
		
		return view;
	}	
}